﻿#region

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Schema;
using InfoTech.VinhSon.Bussiness;

#endregion

namespace VinhSon.Controllers
{
    public class HomeModuleController : BasePageController
    {
        public ActionResult Basic(Group group)
        {
            IEnumerable<Article> q1 = UnitOfWork.GroupRepository.Select(c => c.Inactive == false && c.Id == group.Id)
                               .SelectMany(c => c.Articles)
                               .Where(c => c.Inactive == false || c.Inactive == null)
                               .OrderByDescending(c => c.LastModified);
            if (group.TopArticle.HasValue)
                if (group.TopArticle.Value > 0)
                    q1 = q1.Take(group.TopArticle.Value);
            return PartialView(group.Layout.Action,
                               q1);
        }

        public ActionResult FilterDate(Group group)
        {
            IEnumerable<Article> q1 = UnitOfWork.ArticleGroupRepository.Select(c => c.Inactive == false && c.Group.Id == group.Id && (c.FromDate == null || c.FromDate <= DateTime.Now) && (c.ToDate == null || c.ToDate >= DateTime.Now))
                               .Select(c => c.Article)
                               .Where(c => c.Inactive == false || c.Inactive == null)
                               .OrderByDescending(c => c.LastModified);
            if (group.TopArticle.HasValue)
                if (group.TopArticle.Value > 0)
                    q1 = q1.Take(group.TopArticle.Value);
            return PartialView(group.Layout.Action,
                               q1);
        }

        public ActionResult Top5Article()
        {
            return PartialView(UnitOfWork.ArticleRepository.Select(c => c.Inactive == false && c.Activated == true,
                                                                   c => c.LastModified,
                                                                   ListSortDirection.Descending)
                                         .Take(5));
        }

        public ActionResult SideBar()
        {
            return PartialView(UnitOfWork.CategoryRepository.Select(c => c.IsShowMenu == true && c.Activated == true && c.Inactive == false && c.Parent == null,
                                                                    "Categories")
                                         .ToList());
        }

    }
}
